package com.yi.mall.user.controller;

import com.yi.mall.common.utils.JWTUtils;
import com.yi.mall.common.utils.ResponseEnum;
import com.yi.mall.common.utils.Result;
import com.yi.mall.common.model.Login;
import com.yi.mall.common.model.UserInfo;
import com.yi.mall.user.service.impl.LoginImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController

public class LoginController {
    @Autowired
    private LoginImpl loginservice;
    //登录
    @PostMapping("/login")
    public Result<UserInfo> login(@RequestBody Login login, HttpServletRequest request, HttpServletResponse response){
        UserInfo userInfo = loginservice.login(login, request);
        Map<String,Object> claims= new HashMap<>();
        claims.put("userId",userInfo.getId());
        String token = JWTUtils.generateToken(claims);
        response.setHeader("Authorization",token);
       return Result.success(ResponseEnum.SUCCESS,userInfo);
    }
}
